package demo.service;

import demo.dao.HigherDao;
import demo.dao.TopDao;
import demo.entity.employees;
import demo.entity.managers;
import demo.entity.record;
import demo.mappers.recordRowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;

@Service
public class HigherService {
    @Autowired
    private HigherDao hDao;
    public List<employees> queryAllEmployees(int emp_id){
        String depart= hDao.queryEmployeesById(emp_id).getDepartment_id();
        return hDao.queryAllEmployees(depart);
    }

    public employees changeBasic_Salary(int emp_id, int target_id, int newBasic_Salary){
        if(hDao.queryEmployeesById(target_id).getDepartment_id().equals(hDao.queryEmployeesById(emp_id).getDepartment_id())){
            return hDao.changeBasic_Salary(emp_id,newBasic_Salary);
        }
        else return hDao.worning();

    }
    public employees changeMonthly_Salary(int emp_id, int target_id,int newMonthly_Salary){
        if(hDao.queryEmployeesById(target_id).getDepartment_id().equals(hDao.queryEmployeesById(emp_id).getDepartment_id())){
            return hDao.changeMonthly_Salary(emp_id,newMonthly_Salary);
        }
        else return hDao.worning();
    }
    public employees changeArrival_time(int emp_id,  int target_id,String newArrival_time){
        if(hDao.queryEmployeesById(target_id).getDepartment_id().equals(hDao.queryEmployeesById(emp_id).getDepartment_id())){
            return hDao.changeArrival_time(emp_id,newArrival_time);
        }
        else return hDao.worning();
    }
    public employees changeDeparture_time(int emp_id,  int target_id,String newDeparture_time){
        if(hDao.queryEmployeesById(target_id).getDepartment_id().equals(hDao.queryEmployeesById(emp_id).getDepartment_id())){
            return hDao.changeDeparture_time(emp_id,newDeparture_time);
        }
        else return hDao.worning();
    }
    public List<record> queryAllRecord(int emp_id) {
        String depart = hDao.queryEmployeesById(emp_id).getDepartment_id();
        return hDao.queryAllRecord(depart);
    }
    public employees changeRestHoliday(int emp_id, int newRestHoliday){
        return hDao.changeRestHoliday(emp_id,newRestHoliday);
    }
    public managers addManagers(managers man) {
        employees emp=hDao.queryEmployeesById(man.getEmp_id());
        int restHoliday=emp.getRest_holiday();
        LocalDate start = LocalDate.parse(man.getStart_date());
        LocalDate end = LocalDate.parse(man.getEnd_date());
        Period p = Period.between(start,end);
        int interval = 365 * p.getYears() + 30 * p.getMonths() + p.getDays();
        if(restHoliday<interval){return hDao.no_Moreworning();} //请假天数超过剩余假期
        return hDao.addManagers(man);
    }
    public List<managers> queryManagers(int emp_id){
        String department = hDao.queryEmployeesById(emp_id).getDepartment_id();
        return hDao.queryManagers(department);
    }
    public managers queryManagersById(int emp_id,int id){
        String department= hDao.queryEmployeesById(emp_id).getDepartment_id();
        return hDao.queryManagersById(id,department);
    }

    public managers dealManagers(int emp_id,int id,String status){
        if(hDao.queryEmployeesById(emp_id).getDepartment_id().equals(queryManagersById(emp_id,id).getDepartment_id())&&hDao.queryAllManagersById(id).getJob_id().equals("普通员工")){
            if(status.equals("同意")){
                LocalDate start = LocalDate.parse(queryManagersById(emp_id,id).getStart_date());
                LocalDate end = LocalDate.parse(queryManagersById(emp_id,id).getEnd_date());
                Period p = Period.between(start,end);
                int interval = 365 * p.getYears() + 30 * p.getMonths() + p.getDays();
                System.out.println(interval);
                System.out.println(hDao.queryEmployeesById(queryManagersById(emp_id,id).getEmp_id()).getRest_holiday());
                changeRestHoliday(queryManagersById(emp_id,id).getEmp_id(),hDao.queryEmployeesById(queryManagersById(emp_id,id).getEmp_id()).getRest_holiday()-interval);
            }
            return hDao.dealManagers(id,status);
        }
        else return hDao.managers_Worning();
    }
}
